const { Blog, User } = require("./model.js");
!(async function() {
  // 查询一条记录
  const zhangsan = await User.findOne({
    where: {
      userName: "zhangsan"
    }
  });
  // console.log("zhangsan", zhangsan.dataValues);
  // 查询特定的列
  const zhangsanNames = await User.findOne({
    attributes: ["userName", "nickName"],
    where: {
      userName: "zhangsan"
    }
  });
  console.log("zhangsanNames", zhangsanNames.dataValues);
  // 查询一个列表
  const zhangsanBlogList = await Blog.findAll({
    where: {
      userId: 1
    },
    order: [["id", "desc"]] //倒序
  });
  console.log(
    "zhangsanBlogList",
    zhangsanBlogList.map(blog => blog.dataValues)
  );
  // 分页
  const blogPageList = await Blog.findAll({
    limit: 2,
    offset: 0,
    order: [["id", "desc"]]
  });
  console.log(
    "blogPageList",
    blogPageList.map(blog => blog.dataValues)
  );
  // 查询总数,数据总数
  const blogListAndCount = await Blog.findAndCountAll({
    limit: 2,
    offset: 0,
    order: [["id", "desc"]]
  });
  console.log(
    "blogListAndCount",
    blogListAndCount.count,
    blogListAndCount.rows.map(blog => blog.dataValues)
  );
})();
